package view;

import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.PostMotemModel;
import model.complaintModel;

import common.loadData;
import dao.PostMotemDao;
import dao.complaintdao;

public class panelPostmortem extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel panelTitle = null;
	private JLabel lbtitle = null;
	private JScrollPane ScrollPanelistpostmortem = null;
	private JTable TablePostmortem = null;
	private JButton ButtonAdd = null;
	private JButton ButtonModify = null;
	private JButton ButtonDelete = null;
	public static int idpost=-1;
	JFrame a;
	public panelPostmortem(JFrame a) {
		super();
		this.a = a;
		initialize();
		
	}

	private void initialize() {
		this.setSize(735, 520);
		this.setLayout(null);
		this.add(getPanelTitle(), null);
		this.add(getScrollPanelistpostmortem(), null);
		this.add(getButtonAdd(), null);
		this.add(getButtonModify(), null);
		this.add(getButtonDelete(), null);
	}

	private JPanel getPanelTitle() {
		if (panelTitle == null) {
			lbtitle = new JLabel();
			lbtitle.setLocation(new Point(0, 0));
			lbtitle.setFont(new Font("Dialog", Font.BOLD, 24));
			lbtitle.setForeground(new Color(0, 153, 153));
			lbtitle.setText("List Postmortems");
			lbtitle.setSize(new Dimension(203, 34));
			panelTitle = new JPanel();
			panelTitle.setLayout(null);
			panelTitle.setBounds(new Rectangle(269, 15, 203, 33));
			panelTitle.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelTitle.add(lbtitle, null);
		}
		return panelTitle;
	}

	private JScrollPane getScrollPanelistpostmortem() {
		if (ScrollPanelistpostmortem == null) {
			ScrollPanelistpostmortem = new JScrollPane();
			ScrollPanelistpostmortem.setBounds(new Rectangle(44, 59, 646, 389));
			ScrollPanelistpostmortem.setViewportView(getTablePostmortem());
		}
		return ScrollPanelistpostmortem;
	}

	private JTable getTablePostmortem() {
			TablePostmortem = new JTable(loadData.rowPostmortem, loadData.columnPostmortem);
			if(TablePostmortem.getRowCount()==0)
			{
				JOptionPane.showMessageDialog(null, "No data for keyword '"+mainForm.search+"'!");
			}
		return TablePostmortem;
	}

	private JButton getButtonAdd() {
		if (ButtonAdd == null) {
			ButtonAdd = new JButton();
			ButtonAdd.setIcon(new ImageIcon(getClass().getResource("/image/Create.png")));
			ButtonAdd.setSize(new Dimension(100, 33));
			ButtonAdd.setLocation(new Point(353, 465));
			ButtonAdd.setText("Add");
			ButtonAdd.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					idpost=-1;
					addPostmortem pos = new addPostmortem();
					pos.setVisible(true);
					a.dispose();
				}
			});
		}
		return ButtonAdd;
	}

	private JButton getButtonModify() {
		if (ButtonModify == null) {
			ButtonModify = new JButton();
			ButtonModify.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonModify.setIcon(new ImageIcon(getClass().getResource("/image/Modify.png")));
			ButtonModify.setText("Modify");
			ButtonModify.setLocation(new Point(471, 466));
			ButtonModify.setSize(new Dimension(100, 33));
			ButtonModify.setHorizontalAlignment(SwingConstants.LEFT);
			ButtonModify.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if((TablePostmortem.getSelectedRow())>=0)
					{
					int row = TablePostmortem.getSelectedRow();
					int column = 0;
					idpost = Integer.parseInt(TablePostmortem.getValueAt(row, column).toString());
						addPostmortem com = new addPostmortem();
						com.setTitle("Update Postmortem");
						com.setVisible(true);
						a.dispose();
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Choice row!");
					}
				}
			});
		}
		return ButtonModify;
	}

	private JButton getButtonDelete() {
		if (ButtonDelete == null) {
			ButtonDelete = new JButton();
			ButtonDelete.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonDelete.setIcon(new ImageIcon(getClass().getResource("/image/Delete.png")));
			ButtonDelete.setText("Delete");
			ButtonDelete.setSize(new Dimension(100, 33));
			ButtonDelete.setLocation(new Point(589, 466));
			ButtonDelete.setHorizontalAlignment(SwingConstants.LEFT);
			if(login.idrole==1)
			{
				ButtonDelete.setEnabled(false);
			}
			ButtonDelete.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					int row = TablePostmortem.getSelectedRow();
					int column = 0;
					idpost = Integer.parseInt(TablePostmortem.getValueAt(row, column).toString());
					PostMotemModel model = new PostMotemModel();
					model.setId(idpost);
					int rsms = JOptionPane.showConfirmDialog(null, "Do you want to delete this Postmortem?");
					if(rsms==0)
					{
						int rs=PostMotemDao.deletePostMotem(model);
						if(rs==1)
						{
							JOptionPane.showMessageDialog(null,"Delete Postmortem sucessful!");
							a.dispose();
							loadData.tab=5;
							(new mainForm()).setVisible(true);
						}
						else
						{
							JOptionPane.showMessageDialog(null,"Delete Postmortem fail!");
						}
					}
				}
			});
		}
		return ButtonDelete;
	}

}
